package com.mjl.shops.fragment;

import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jaeger.library.StatusBarUtil;
import com.mjl.shops.R;
import com.mjl.shops.activity.SearchActivity;
import com.mjl.shops.activity.SecondClassActivity;
import com.mjl.shops.adapter.HomeTuiJianAdapter;
import com.mjl.shops.base.BaseFragment;
import com.mjl.shops.bean.HomeBanner;
import com.mjl.shops.bean.HomeTuiJian;
import com.mjl.shops.utils.CommUtils;
import com.mjl.shops.utils.ImgUtils;
import com.mjl.shops.view.NiceImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class HomeFragment extends BaseFragment {

    private List<HomeTuiJian> tuiJians = new ArrayList<>();

    private HomeTuiJianAdapter adapter;

    @BindView(R.id.search_box_rl)
    RelativeLayout mSearchBox;

    @BindView(R.id.banner)
    NiceImageView banner;

    @BindView(R.id.right_top_adver_img)
    NiceImageView right_top_adver_img;

    @BindView(R.id.right_bottom_adver_img)
    NiceImageView right_bottom_adver_img;

    @BindView(R.id.left_adver_img)
    NiceImageView left_adver_img;

    @BindView(R.id.tuijian_recycler)
    RecyclerView tuijian_recycler;

    @Override
    protected View initView() {
        StatusBarUtil.setTranslucentForImageView(getActivity(), 10, null);
        StatusBarUtil.setDarkMode(getActivity());
        View mView = View.inflate(getActivity(), R.layout.fragment_home_layout, null);
        ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    protected void setData(View view) {

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        adapter = new HomeTuiJianAdapter(getActivity(), R.layout.item_image_layout, tuiJians);
        tuijian_recycler.setLayoutManager(gridLayoutManager);
        tuijian_recycler.setAdapter(adapter);
    }

    @Override
    protected void initData() {

        //顶部banner图
        BmobQuery<HomeBanner> bmobQuery = new BmobQuery<HomeBanner>();
        bmobQuery.addQueryKeys("url");
        bmobQuery.findObjects(new FindListener<HomeBanner>() {
            @Override
            public void done(List<HomeBanner> list, BmobException e) {
                if (e == null) {
                    //注意：这里的Person对象中只有指定列的数据。
                    if (list != null && list.size() > 0) {
                        banner.setVisibility(View.VISIBLE);
                        String url = list.get(CommUtils.getRandom(list.size())).getUrl();
                        ImgUtils.ImageShow(getContext(), url, banner);

                        if (list.size() > 0) {
                            ImgUtils.ImageShow(getContext(), list.get(0).getUrl(), right_top_adver_img);
                        }

                        if (list.size() > 1) {
                            ImgUtils.ImageShow(getContext(), list.get(1).getUrl(), right_bottom_adver_img);
                        }
                    }
                } else {
                    banner.setVisibility(View.GONE);
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });

        ImgUtils.ImageShow(getContext(), "https://tu1.whhost.net/uploads/20181201/16/1543652762-UPctDzponO.jpeg", left_adver_img);

        BmobQuery<HomeTuiJian> tuijian = new BmobQuery<HomeTuiJian>();
        tuijian.addQueryKeys("url");
        tuijian.findObjects(new FindListener<HomeTuiJian>() {
            @Override
            public void done(List<HomeTuiJian> list, BmobException e) {
                if (e == null) {
                    if (list != null && list.size() > 0) {
                        tuiJians.clear();
                        tuiJians.addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @OnClick({R.id.search_box_rl, R.id.first_type_item_ll, R.id.second_type_item_ll, R.id.third_type_item_ll, R.id.fourth_type_item_ll})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.search_box_rl:
                SearchActivity.StartIntent(getActivity());
                break;
            case R.id.first_type_item_ll:
                SecondClassActivity.StartIntent(getActivity(),"休闲食品");
                break;
            case R.id.second_type_item_ll:
                SecondClassActivity.StartIntent(getActivity(),"乳品饮料");
                break;
            case R.id.third_type_item_ll:
                SecondClassActivity.StartIntent(getActivity(),"水产海鲜");
                break;
            case R.id.fourth_type_item_ll:
                SecondClassActivity.StartIntent(getActivity(),"新鲜水果");
                break;
            default:
                break;
        }
    }
}
